//
//  SEGPhoneRechargeView.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/2/28.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGPhoneRechargeView.h"

@implementation SEGPhoneRechargeView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = kColor_W;
        self.userInteractionEnabled = YES;
        
        [self initSubviews];
    }
    return self;
}

- (void)initSubviews
{
    CGFloat height = 429.5 + 120 * (LYScreenWidth()/375.0);
    self.contentSize = CGSizeMake(LYScreenWidth(), height);
    
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = kColor_W;
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self);
        make.height.mas_equalTo(height);
        make.width.mas_equalTo(LYScreenWidth());
    }];
    
    self.topBgView = [[UIImageView alloc] init];
    self.topBgView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"img_chongzhi_01")];
    [self.contentView addSubview:self.topBgView];
    [self.topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.contentView).with.offset(60);
        make.height.mas_equalTo(100);
    }];
    
    //中间的填写视图
    self.fillView = [[UIView alloc] init];
    [self.contentView addSubview:self.fillView];
    self.fillView.userInteractionEnabled = YES;
    [self.fillView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topBgView.mas_bottom).with.offset(60);
        make.left.equalTo(self.contentView).with.offset(15);
        make.right.equalTo(self.contentView).with.offset(-15);
//        make.height.mas_equalTo(160);
    }];
    
    self.phoneText = [[UITextField alloc] init];
    self.phoneText.backgroundColor = kColor_L3;
    [self.phoneText setTextColor:kColor_3];
    [self.phoneText setFont:[UIFont systemFontOfSize:13]];
    [self.phoneText setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"请输入需要充值的手机号码" attributes:@{NSForegroundColorAttributeName:kColor_3}]];
    self.phoneText.layer.cornerRadius = 2;
    self.phoneText.layer.masksToBounds = YES;
    self.phoneText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 88)];
    self.phoneText.leftViewMode = UITextFieldViewModeAlways;
    self.phoneText.delegate = self;
    self.phoneText.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.fillView addSubview:self.phoneText];
    [self.phoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.fillView);
        make.height.mas_equalTo(44);
    }];
    
    UIButton *commitButton = [[UIButton alloc] init];
    commitButton.backgroundColor = kColor_T;
    [commitButton setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
    [commitButton setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
    [commitButton setTitle:@"提交手机号码" forState:UIControlStateNormal];
    commitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [commitButton setTitleColor:kColor_W forState:UIControlStateNormal];
    commitButton.layer.masksToBounds = YES;
    commitButton.layer.cornerRadius = 2;
    [commitButton addTarget:self action:@selector(commitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.fillView addSubview:commitButton];
    [commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.fillView);
        make.top.equalTo(self.phoneText.mas_bottom).with.offset(15);
        make.height.mas_equalTo(44);
    }];
    
    UILabel *descLabel = [[UILabel alloc] init];
    descLabel.textColor = kColor_3;
    descLabel.font = [UIFont systemFontOfSize:13];
    descLabel.text = @"请务必确保你要充值的手机号码的正确性，充值的手机号码以你提交后的号码为准。";
    descLabel.numberOfLines = 0;
    [self.fillView addSubview:descLabel];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.fillView);
        make.top.equalTo(commitButton.mas_bottom).with.offset(15);
        make.bottom.equalTo(self.fillView);
    }];
    
    //底部背景图片
    UIImageView *bottomBgView = [[UIImageView alloc] init];
//    bottomBgView.backgroundColor = kColor_S1;
    [self.contentView addSubview:bottomBgView];
    [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(15);
        make.right.equalTo(self.contentView).with.offset(-15);
        make.top.equalTo(self.fillView.mas_bottom).with.offset(60);
        make.height.mas_equalTo(120 * (LYScreenWidth()/375.0));//广告图片要等比拉伸
    }];
    self.adImageView = bottomBgView;
    
    UIButton *adButton = [[UIButton alloc] init];
    [bottomBgView addSubview:adButton];
    [adButton setTitle:@" 广告 " forState:UIControlStateNormal];
    [adButton.titleLabel setFont:[UIFont systemFontOfSize:10]];
    [adButton setTitleColor:kColor_3 forState:UIControlStateNormal];
    [adButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"img_bannner_ad_bg")] forState:UIControlStateNormal];
    [adButton mas_makeConstraints:^(MASConstraintMaker* make) {
        make.bottom.equalTo(bottomBgView).mas_offset(-5);
        make.right.equalTo(bottomBgView).mas_offset(-5);
    }];
}

//提交完成 显示完成界面
- (void)showFinishViewWithApplyTime:(NSString *)applyTime
{   //隐藏中间的填写视图
    [self.fillView removeFromSuperview];
    
    if (self.finishView) {
        return;
    }
    self.finishView = [[UIView alloc] init];
    [self.contentView addSubview:self.finishView];
    self.finishView.userInteractionEnabled = YES;
    [self.finishView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topBgView.mas_bottom).with.offset(60);
        make.left.equalTo(self.contentView).with.offset(15);
        make.right.equalTo(self.contentView).with.offset(-15);
    }];
    
    UILabel *descLabel = [[UILabel alloc] init];
    descLabel.textColor = kColor_2;
    descLabel.font = [UIFont systemFontOfSize:13];
    descLabel.numberOfLines = 0;
    [self.finishView addSubview:descLabel];
    [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.finishView);
    }];
    self.descLabel = descLabel;
    self.descLabel.text = [NSString stringWithFormat:@"手机号码已提交，你充值的手机话费将在两个工作日内到账，请注意查收。\n提交时间：%@",applyTime];
    
    self.contactServiceButton = [[UIButton alloc] init];
    self.contactServiceButton.backgroundColor = kColor_W;
    [self.contactServiceButton setTitle:@"联系客服" forState:UIControlStateNormal];
    self.contactServiceButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.contactServiceButton setTitleColor:kColor_2 forState:UIControlStateNormal];
    self.contactServiceButton.layer.masksToBounds = YES;
    self.contactServiceButton.layer.cornerRadius = 4;
    self.contactServiceButton.layer.borderWidth = 0.5;
    self.contactServiceButton.layer.borderColor = kColor_L2.CGColor;
    [self.contactServiceButton addTarget:self action:@selector(contactServiceButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.finishView addSubview:self.contactServiceButton];
    [self.contactServiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.finishView);
        make.top.equalTo(descLabel.mas_bottom).with.offset(45);
        make.height.mas_equalTo(44);
        make.bottom.mas_equalTo(self.finishView);
    }];
    
    //调整广告图片位置
    [self.adImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.finishView.mas_bottom).with.offset(60);
    }];
    
    //调整整个视图高度
    CGFloat height = 416 + 120 * (LYScreenWidth()/375.0);
    self.contentSize = CGSizeMake(LYScreenWidth(), height);
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
}

- (void)updateAdImageWithString:(NSString *)string
{
    
    [self.adImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kClientImageRootURL,string]]];
}


//限制UITextField输入长度
//最大输入长度为11个字符
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
    if (string.length == 0)
        return YES;
    
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    if (existedLength - selectedLength + replaceLength > 11) {
        return NO;
    }else {
        return YES;
    }
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
    [self commitButtonClick];
    return YES;
}
//提交手机号
- (void)commitButtonClick
{
    [self.phoneText resignFirstResponder];
    if (self.phoneText.text.length != 11) {
        [SEGAlertView showTmepMessageWithMessage:kPhoneError];
    }else {
        if (self.viewDelegate && [self.viewDelegate respondsToSelector:@selector(showConfirmViewWithPhone:)]) {
            [self.viewDelegate showConfirmViewWithPhone:self.phoneText.text];
        }
    }
}
//联系客服
- (void)contactServiceButtonClick
{
    if (self.viewDelegate && [self.viewDelegate respondsToSelector:@selector(contactCustomerService)]) {
        [self.viewDelegate contactCustomerService];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.phoneText resignFirstResponder];
}

@end
